<template>
  <el-container class="layout-cont" id="watermarkRef">
    <el-aside class="main-cont main-left">
      <div v-if="isSider" class="menu_title">管理后台</div>
      <Aside class="aside" />
    </el-aside>
    <el-container
      :class="[isSider ? 'openside' : 'hideside', isMobile ? 'mobile' : '']"
    >
      <el-header class="header-cont">
        <div class="title">
          <div @click.stop="totalCollapse" class="menu-total">
            <i class="el-icon-s-unfold" v-if="isCollapse"></i>
            <i class="el-icon-s-fold" v-else></i>
          </div>
          <h2 class="tit-text">内蒙应急指挥系统信息维护系统</h2>
        </div>
        <div class="fl-right right-box">
          <!-- <el-link class="right-link" :underline="false">风险看板</el-link>
          <el-link class="right-link" :underline="false"
            >粤政易风险看板</el-link
          >-->

          <el-popover placement="bottom-start" width="300" trigger="hover">
            <div class="popover-container">
              <div class="popover-title">
                <span>账户信息</span>
                <el-button type="text">个人中心</el-button>
              </div>
              <el-divider></el-divider>
              <p class="popover-text">
                {{ `姓名：${(userInfo && userInfo.realName) || "--"}` }}
              </p>
              <el-divider></el-divider>
              <p class="popover-text">
                {{ `联系电话：${(userInfo && userInfo.mobile) || "--"}` }}
              </p>
              <el-divider></el-divider>
              <div class="popover-footer">
                <el-button type="primary" @click="LoginOut(1)">退出</el-button>
              </div>
            </div>
            <span class="header-avatar" slot="reference">
              <!-- <CustomPic /> -->
              <SvgIcon
                icon-class="person"
                font-size="18px"
                color="#3066dd"
                style="border: 2px solid #fff; padding: 5px; border-radius: 50%"
              ></SvgIcon>
              <span style="margin-left: 5px">
                {{ userInfo && userInfo.realName }}
              </span>
            </span>
          </el-popover>
          <el-divider direction="vertical"></el-divider>
          <el-dropdown @command="switchRoles">
            <span class="dropdown-link">
              {{ rolesInfo || "--" }}
              <i class="el-icon-arrow-down"></i>
            </span>
            <el-dropdown-menu
              slot="dropdown"
              style="max-height: 80vh; overflow-x: auto"
            >
              <el-dropdown-item
                v-if="rolesList.Permission && rolesList.Permission.length < 1"
                >暂无其它可用角色</el-dropdown-item
              >
              <el-dropdown-item
                v-for="(item, index) in rolesList.Permission"
                :key="index"
                :command="item.id"
              >
                {{ (item.org && item.org.name) || "" }}
                {{ item.role && item.role.name }}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-badge is-dot  class="message-doc">
            <i class="el-icon-message"></i>
          </el-badge> -->
          <!-- <i class="el-icon-date"></i> -->
        </div>
      </el-header>

      <!-- 分块滑动功能 -->
      <el-main class="main-cont main-right">
        <transition
          :duration="{ enter: 800, leave: 100 }"
          mode="out-in"
          name="el-fade-in-linear"
        >
          <div class="topfix">
            <!-- 当前面包屑用路由自动生成可根据需求修改 -->
            <!-- :to="{ path: item.path }" 暂时注释不用-->
            <HistoryComponent />
          </div>
        </transition>
        <transition mode="out-in" name="el-fade-in-linear">
          <keep-alive>
            <router-view
              v-loading="loadingFlag"
              element-loading-text="正在加载中"
              class="admin-box"
              v-if="$route.meta.keepAlive && reloadFlag"
            ></router-view>
          </keep-alive>
        </transition>
        <transition mode="out-in" name="el-fade-in-linear">
          <router-view
            v-loading="loadingFlag"
            element-loading-text="正在加载中"
            class="admin-box"
            v-if="!$route.meta.keepAlive && reloadFlag"
          ></router-view>
        </transition>
        <!-- <BottomInfo /> -->
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import Vue from "vue";
import Aside from "./aside";
import HistoryComponent from "./aside/historyComponent/history";
// import BottomInfo from '@/view/layout/bottomInfo/bottomInfo'
import { mapActions, mapGetters } from "vuex";
import store  from "@/store/index.js";
// import CustomPic from "@/components/customPic";
// import watermarker from "js-watermarker";
export default {
  name: "Layout",
  data() {
    return {
      show: false,
      isCollapse: false,
      isSider: true,
      isMobile: false,
      isShadowBg: false,
      loadingFlag: false,
      reloadFlag: true,
      // userInfo: {},
    };
  },
  components: {
    Aside,
    HistoryComponent,
  },
  methods: {
    ...mapActions("user", ["LoginOut", "LoginIn", "changeRole"]),
    reload() {
      this.reloadFlag = false;
      this.$nextTick(() => {
        this.reloadFlag = true;
      });
    },
    totalCollapse() {
      this.isCollapse = !this.isCollapse;
      this.isSider = !this.isCollapse;
      this.isShadowBg = !this.isCollapse;
      this.$bus.emit("collapse", this.isCollapse);
    },
    toPerson() {
      this.$router.push({ name: "person" });
    },
    changeShadow() {
      this.isShadowBg = !this.isShadowBg;
      this.isSider = !!this.isCollapse;
      this.totalCollapse();
    },
    switchRoles(id) {
      if (!id) {
        return;
      }
      this.changeRole({ id }).then((res) => {
        console.log("changeRole()", res);
        if (res.code == 0) {
          console.log("changeRole", res);
          sessionStorage.removeItem("historys");
          sessionStorage.removeItem("activeValue");
          this.$bus.emit("changeRole", res);
        }
      });
      // let result = await this.$store.dispatch('user/changeRole',{id})
    },
  },
  computed: {
    ...mapGetters("user", ["rolesList", "rolesInfo", "userInfo"]),
    title() {
      return this.$route.meta.title || "管理后台";
    },
    matched() {
      return this.$route.matched;
    },
  },
  created() {
    console.log("vuex存储的用户信息：", store.getters["user/userInfo"]);
    // this.userInfo = Vue.ls.get("userInfo");
    // console.log("ls存储的用户信息：", this.userInfo);
  },
  mounted() {
    //添加水印
    // console.log(watermarker);
    // let waterName = this.rolesList.realName + " | " + this.rolesList.mobile;
    // this.$nextTick(() => {
    //   const marker = watermarker.init(document.getElementById("watermarkRef"));
    //   const options = {
    //     content: [waterName],
    //     textStyle: {
    //       left: 20,
    //       top: 100,
    //       rotate: -10,
    //       align: "left",
    //       fontSize: 16,
    //       lineHeight: 25,
    //       color: "#ccc",
    //       alpha: 0.6,
    //     },
    //     imageStyle: {
    //       width: 600,
    //       height: 400,
    //       position: "left top",
    //       repeat: "repeat",
    //     },
    //     zIndex: 9999,
    //     debug: false,
    //   };
    //   marker.setOption(options);
    // });

    let screenWidth = document.body.clientWidth;
    if (screenWidth < 1000) {
      this.isMobile = true;
      this.isSider = false;
      this.isCollapse = true;
    } else if (screenWidth >= 1000 && screenWidth < 1200) {
      this.isMobile = false;
      this.isSider = false;
      this.isCollapse = true;
    } else {
      this.isMobile = false;
      this.isSider = true;
      this.isCollapse = false;
    }
    this.$bus.emit("collapse", this.isCollapse);
    this.$bus.emit("mobile", this.isMobile);
    this.$bus.on("reload", this.reload);
    this.$bus.on("showLoading", () => {
      this.loadingFlag = true;
    });
    this.$bus.on("closeLoading", () => {
      this.loadingFlag = false;
    });
    window.onresize = () => {
      return (() => {
        let screenWidth = document.body.clientWidth;
        if (screenWidth < 1000) {
          this.isMobile = true;
          this.isSider = false;
          this.isCollapse = true;
        } else if (screenWidth >= 1000 && screenWidth < 1200) {
          this.isMobile = false;
          this.isSider = false;
          this.isCollapse = true;
        } else {
          this.isMobile = false;
          this.isSider = true;
          this.isCollapse = false;
        }
        this.$bus.emit("collapse", this.isCollapse);
        this.$bus.emit("mobile", this.isMobile);
      })();
    };
  },
};
</script>
<style lang="scss" scope>
@import "@/style/mobile.scss";
.el-divider--vertical {
  margin: 0 15px !important;
}
.topfix {
  position: sticky;
  top: 0px;
}
.popover-container {
  .popover-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .popover-text {
    height: 40px;
    line-height: 40px;
  }
  .popover-footer {
    display: flex;
    justify-content: center;
    button {
      width: 280px;
    }
  }
  .el-divider--horizontal {
    margin: 8px 0;
  }
}
.header-avatar {
  cursor: pointer;
}
.dropdown-link {
  // margin-left: 15px;
  cursor: pointer;
}
.message-doc {
  margin: 0 15px;
  height: 18px;
  line-height: 18px;
  cursor: pointer;
  .el-badge__content.is-dot {
    height: 6px;
    width: 6px;
  }
}
.el-icon-date,
.el-icon-message {
  font-size: 20px;
}
</style>
